<?php

namespace App\Admin\Controllers;

use App\Admin\Actions\Post\HouseType;
use App\Admin\Actions\Post\WxUser;
use App\Models\RentAndPurchase;
use App\Tools\TxdtTools;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;

class RentAndPurchaseController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new RentAndPurchase(), function (Grid $grid) {
            $grid->column('id')->sortable();
            $grid->column('uid','用户')->display(function ($grid){
                $user = \App\Models\WxUser::where('id',$grid)->first()->toArray();
                return $user['user_name'];
            });;
            $grid->column('house_name','小区名称');
            $grid->column('house_type','房源类型')->display(function ($grid){
                $type = \App\Models\HouseType::where('id',$grid)->first(['house_type_name']);
                return $type->house_type_name;
            });
            $grid->column('house_region','房源区域')->display(function ($grid){
                $tx = TxdtTools::staticAddressList($grid);
                return $tx;
            });
            $grid->column('house_product','房源预算');
            $grid->column('house_budget','房源面积');
            $grid->column('house_rent_and_purchase','租房或购房')->display(function ($grid){
                if($grid==1){
                        return "租房";
                }else{
                        return "购房";
                }
            });
            $grid->column('house_title','标题');
            $grid->column('house_text','描述');
            $grid->column('house_end_time','结束时间');
            $grid->column('created_at');
            $grid->column('updated_at')->sortable();

            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('id');

            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new RentAndPurchase(), function (Show $show) {
            $show->field('uid','用户');
            $show->field('house_name','小区名称');
            $show->field('house_type','房源类型');
            $show->field('house_region','房源区域');
            $show->field('house_product','房源预算');
            $show->field('house_budget','房源面积');
            $show->field('house_rent_and_purchase','租房或购房');
            $show->field('house_title','标题');
            $show->field('house_text','描述');
            $show->field('house_end_time','结束时间');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new RentAndPurchase(), function (Form $form) {
            $form->multipleSelectTable('uid','用户')->from(WxUser::make())->saving(function ($saving){
                return implode(',',$saving);
            });
            $form->multipleSelectTable('house_type',"商品类型")->from(HouseType::make())->saving(function ($saving){
                return implode(',',$saving);
            });
            $form->text('house_name','小区名称');
            $form->text('house_region','房源区域');
            $form->text('house_product','房源预算');
            $form->text('house_budget','房源面积');
            $form->text('house_rent_and_purchase','租房或购房');
            $form->text('house_title','标题');
            $form->textarea('house_text','描述');
            $form->date('house_end_time','结束时间');
            $form->display('created_at');
            $form->display('updated_at');
//            $form->submitted(function (Form $saving){
//                $saving->house_region = (new TxdtTools())->addressName($saving->house_region);
//                dd($saving);
//            });
        });
    }
}
